package cn.train.controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.train.service.AddOrderTableService;
import cn.train.service.QueryCXService;

@WebServlet("/AddOrderTableServlet")
public class AddOrderTableServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		/*
		 * HttpSession session = req.getSession(); String
		 * name=(String)session.getAttribute("name"); String
		 * uname=(String)session.getAttribute("uname"); String
		 * idcard=(String)session.getAttribute("idcard");
		 */
		//System.out.println(111111);
		String renshu=req.getParameter("renshu");
		String xinxinum = req.getParameter("xinxinum");//1，单程2，往返
		String names = req.getParameter("name");//多个购票人的姓名
		String zongjiaqian = req.getParameter("zongjiaqian");
        String uname = req.getParameter("uname");//用户昵称		
		String idcards = req.getParameter("IDcard");//多个购票人身份证号
		
		String action = req.getParameter("action");//起点站
		String over = req.getParameter("over");   //终点站
		String cname = req.getParameter("cname");//车昵称
		String starttime = req.getParameter("starttime");//开始时间
		String endtime = req.getParameter("endtime");  //结束时间
		String zongtime1 = req.getParameter("zongtime1");  //总时间
		String actiondate = req.getParameter("actiondate");//开始日期
		String week1= req.getParameter("week1");//周
		String zuotype = req.getParameter("zuotype");//座位类型
		String price = req.getParameter("price");//单个人票价
		
		String action1 = req.getParameter("action1");//起点站
		String over1 = req.getParameter("over1");   //终点站
		String cname1 = req.getParameter("cname1");//车昵称
		String starttime1 = req.getParameter("starttime1");//开始时间
		String endtime1 = req.getParameter("endtime1");  //结束时间
		String zongtime2 = req.getParameter("zongtime2");  //总时间
		String actiondate1 = req.getParameter("actiondate1");//开始日期
		String week2 = req.getParameter("week2");//开始日期
		
		String zuotype1 = req.getParameter("zuotype1");//座位类型
		String price1 = req.getParameter("price1");//单个人票价
		
		String fla=req.getParameter("flag");
		HttpSession session=req.getSession();
		if("1".equals(fla)){
			xinxinum="单程";
			renshu="1";
			String onum=req.getParameter("onum");
				names=req.getParameter("names");
			cname=req.getParameter("cname");
			action=req.getParameter("action");
			over=req.getParameter("over");
			starttime=req.getParameter("starttime");
			endtime=req.getParameter("endtime");
			actiondate=req.getParameter("actiondate");
			zuotype=req.getParameter("zuotype");
			price=req.getParameter("price");
			zongjiaqian=price;
			PrintWriter  out=resp.getWriter();

			SimpleDateFormat adf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat ad = new SimpleDateFormat("HH:mm:ss");
		 	try { 
	            long hour = 0;  
	            long min = 0;  
	            long sec = 0;  
				java.util.Date date1 = adf.parse(actiondate+" "+starttime);
				java.util.Date date2 = ad.parse(starttime);
				java.util.Date date3 = ad.parse(endtime);
				
				System.out.println("date1===="+date1);
				java.util.Date dat=new java.util.Date();
				System.out.println("dat===="+dat);
				long time1 = dat.getTime();
	            long time2 = date1.getTime();
	            long diff = time1 - time2;
	            long time3=date2.getTime();
	            long time4=date3.getTime();
	            long diff1=time4-time3;
                hour = (diff1 / (60 * 60 * 1000));  
                min = ((diff1 / (60 * 1000)) - hour * 60);  
                sec = (diff1/1000-hour*60*60-min*60);  
	            zongtime1=hour+"小时"+min+"分钟"+sec+"秒";        
	    
	            System.out.println("diff=="+diff);
	            if(diff>0){
	            	System.out.println("133135151321321321321321321");
	            	out.write("<script>alert('订单失效！');window.open('font/pay/vip.jsp')</script>");
	            return;
	            }else{
	            	session.setAttribute("renshu",renshu);
	            	session.setAttribute("onum", onum);
	    			session.setAttribute("action",action);//起点站
	    		    session.setAttribute("over",over);   //终点站
	    			session.setAttribute("cname",cname);//车昵称
	    			session.setAttribute("starttime",starttime);//开始时间
	    			session.setAttribute("endtime",endtime);  //结束时间
	    			session.setAttribute("zongtime1",zongtime1);  //总时间
	    			session.setAttribute("week1",week1);  //总时间
	    			session.setAttribute("actiondate",actiondate);//开始日期
	    			session.setAttribute("zuotype",zuotype);//座位类型
	    			session.setAttribute("price",price);//单个人票价
	    			session.setAttribute("zongjiaqian",zongjiaqian);
	            	 req.getRequestDispatcher("font/pay/payfor.jsp").forward(req, resp);
	         		return;
	            }
	            
		 	
		 	}catch(Exception e){
		e.printStackTrace();	
		}
		}
		
		
		//String state = req.getParameter("state");  状态
		//String onum=req.getParameter("onum");
		//int s = Integer.parseInt(state);
        System.out.println("人数：========"+renshu);
        if(renshu!=null){
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String re = dateFormat.format(new java.util.Date());
		String onum = re;	
		System.out.println("starttime===="+starttime);
		Time stime = Time.valueOf(starttime);
		Time etime = Time.valueOf(endtime);
		Date da = Date.valueOf(actiondate);		
		BigDecimal pric = new BigDecimal(price).setScale(2, BigDecimal.ROUND_HALF_UP);		
		AddOrderTableService addOrederTableService = new AddOrderTableService();
		PrintWriter out = resp.getWriter();
		
		int rennum=Integer.parseInt(renshu);
		String[] namel=names.split(",");
		String[] idcardl=idcards.split(",");
		
		int result=0;
		int result1=0;
		
		for(int i=0;i<rennum;i++){
			System.out.println(namel[i]+","+idcardl[i]+","+1+","+i);
		     result = addOrederTableService.addOrderTable(namel[i], uname, idcardl[i], action, over, cname, stime, etime, da,
				zuotype, pric, 0, onum);
		}
		
		if("往返".equals(xinxinum)){
			Time stime1 = Time.valueOf(starttime1);
			Time etime1 = Time.valueOf(endtime1);
			Date da1 = Date.valueOf(actiondate1);		
			BigDecimal pric1 = new BigDecimal(price1).setScale(2, BigDecimal.ROUND_HALF_UP);
			for(int i=0;i<rennum;i++){
			  System.out.println(namel[i]+","+idcardl[i]+","+2+","+i);
			  result1 = addOrederTableService.addOrderTable(namel[i], uname, idcardl[i], action1, over1, cname1, stime1, etime1, da1,
					zuotype1, pric1, 0, onum);
			}
		}
		System.out.println("result==="+result+",resu=="+result1+","+xinxinum);
		if(result>0||result1>0){
			session.setAttribute("renshu",renshu);
			session.setAttribute("xinxinum",xinxinum);
			session.setAttribute("names",names);//多个购票人的姓名
			session.setAttribute("zongjiaqian",zongjiaqian);
	        session.setAttribute("uname",uname);//用户昵称		
			session.setAttribute("IDcard",idcards);//多个购票人身份证号
			session.setAttribute("onum", onum);
			
			session.setAttribute("action",action);//起点站
		    session.setAttribute("over",over);   //终点站
			session.setAttribute("cname",cname);//车昵称
			session.setAttribute("starttime",starttime);//开始时间
			session.setAttribute("endtime",endtime);  //结束时间
			session.setAttribute("zongtime1",zongtime1);  //总时间
			session.setAttribute("week1",week1);  //总时间
			session.setAttribute("actiondate",actiondate);//开始日期
			session.setAttribute("zuotype",zuotype);//座位类型
			session.setAttribute("price",price);//单个人票价
			
			session.setAttribute("action1",action1);//起点站
			session.setAttribute("over1",over1);   //终点站
			session.setAttribute("cname1",cname1);//车昵称
			session.setAttribute("starttime1",starttime1);//开始时间
			session.setAttribute("endtime1",endtime1);  //结束时间
			session.setAttribute("zongtime2",zongtime2);  //总时间
			session.setAttribute("week2",week2);  //周
			session.setAttribute("actiondate1",actiondate1);//开始日期
			session.setAttribute("zuotype1",zuotype1);//座位类型
			session.setAttribute("price1",price1);//单个人票价
		}
			
		}
		
        req.getRequestDispatcher("font/pay/payfor.jsp").forward(req, resp);
		return;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
